<?php
/**
 * created by zzy
 * date: 2017/10/24 9:28
 */

namespace App\Api\Service\User\Controllers;

use App\Eloquent\Ygt\User;
use Framework\BaseClass\Api\Controller;
use App\Eloquent\Ygt\UserMessage;
use App\Eloquent\Ygt\DepartmentUser;
use App\Engine\Func;
use Framework\Services\Push\JPush\JPushService;

class MessageController extends Controller
{
    public $userId          = 25;
    //消息列表
    public function index()
    {
        $userId             = Func::getHeaderValueByName( 'userid',$this->userId );
        $info               = DepartmentUser::getCurrentInfo( $userId );
        if( !$info ){
            xThrow( ERR_PARAMETER );
        }
        $companyId          = $info->company_id;
        $page               = request( 'page', 1 );
        $limit              = request('limit', 10 );
        $foreignType        = request('type', 0 );
        $startDate          = request('date', 0 );
        $offset             = ( $page - 1 ) * $limit;

        $where              = [ 'company_id'=>$companyId ];
        if($startDate)
        {
            $startTime      = Func::getFormsDateTime( $startDate );
            $endTime        = Func::getFormsDateTime( $startDate, true );
            if($startTime && $endTime)
            {
                $where['created_at']    = [$startTime,$endTime];
            }
        }
        $limit              = $offset = '';
        $data               = UserMessage::getListByForeignType($where,$userId,$foreignType,$limit,$offset);
        $lists              = $data['lists'];
        $count              = $data['count'];
        $readCount          = $data['read_count'];
        $readNumber         = $readCount>0 ? 1 : 0;
        $pageSize           = $limit;
//        $pageSize           = 1;
        $pagingData         = [
            'page'         => $page,
            'pageSize'     => $pageSize,
            'total'         => $count,
//            'total_page'   => ceil($count/$pageSize),
            'total_page'   => 1,
            'read_number'  =>$readNumber,
            'keyword'      => ''
        ];

        return ['__data__' => $lists, '__paging__' => $pagingData];
        //return $this->pagingData($lists, $pagingData);
    }
    //消息列表
    public function lists()
    {
        $userId             = Func::getHeaderValueByName('userid', $this->userId);
        $info               = DepartmentUser::getCurrentInfo($userId);
        $messageId          = request('id',0);
        if( !$info){
            xThrow( ERR_PARAMETER );
        }
        $page               = request( 'page', 1 );
        $limit              = request('limit', 10 );
        $offset             = ( $page - 1 ) * $limit;
        $companyId          = $info->company_id;
        $where['company_id']    = $companyId;
        $limit              = $offset = '';
        $data               = UserMessage::getListByForeignKey($where,$userId,$messageId,$limit,$offset);
        if($data['count'] == 0){
            return [
                'code' => 1,
                'message' => '数据已删除'
            ];
        }

        $lists              = $data['list'];
        $count              = $data['count'];
        $pagingData         = [
            'page'         => 1,
            'pageSize'     => 1,
            'total'         => 1,
            'keyword'      => ''
        ];
        return $this->pagingData($lists, $pagingData);
    }
    public function delete()
    {
        $userId            = Func::getHeaderValueByName( 'userid',$this->userId);
        $messageId         = request('id', 0);
        if( !$userId || !$messageId ){
            xThrow( ERR_PARAMETER );
        }
        $delResult         = UserMessage::deleteOne( $userId, $messageId );
        if( $delResult !== false ){
            $result        = ['message'=>'删除成功'];
            return $result;
        } else {
            xThrow( ERR_UNKNOWN );
        }
    }
    public function read()
    {
        $userId            = Func::getHeaderValueByName( 'userid',$this->userId);
        $messageId         = request('id', 0);
        if( !$userId || !$messageId ){
            xThrow( ERR_PARAMETER );
        }
        $result             = UserMessage::readOne($userId, $messageId);
        if( $result !== false ){

            $where          = ['id'=>$userId];
            $userInfo       = User::getInfo($where);
            if($userInfo)
            {
                $where          = ['is_read'=>0,'to_user_id'=>$userId];
                $messageCount   = UserMessage::getCount($where);
                $iosArr         = ['badge'=>$messageCount];
                $androidArr     = [];
                $jpush          = new JPushService();
                $jpush->sendByMobile( $userInfo->mobile,'',$iosArr,$androidArr );
            }
            $result        = ['badge'=>$messageCount];
            return $result;
        } else {
            xThrow( ERR_UNKNOWN );
        }
    }

    public function messageNumber()
    {
        $userId             = Func::getHeaderValueByName( 'userid',$this->userId);
        $number             = 0;
        if( $userId )
        {
            $where          = ['is_read'=>0,'to_user_id'=>$userId];
            $number         = UserMessage::getCount($where);
        }
        $result             = ['number'=>$number];
        return $result;
    }
    public function changeOne()
    {
        $userId             = Func::getHeaderValueByName('userid',$this->userId);
        $userInfo           = DepartmentUser::getCurrentInfo($userId);
        $requestMessageId   = request('id',0);
        if(!$userInfo)
        {
            xThrow( ERR_PARAMETER,'登录超时' );
        }
        $companyId          = $userInfo->company_id;
        //传过来的消息内容
        $where              = ['id'=>$requestMessageId];
        $requestInfo        = UserMessage::getInfo($where);
        if(!$requestInfo)
        {
//            xThrow( ERR_UNKNOWN,'出错了' );
            $requestInfo = \App\Eloquent\Ygt\UserMessage::withTrashed()->where($where)->first();
            if(!$requestInfo)
            {
                xThrow( ERR_UNKNOWN,'出错了' );
            }
        }
        //查询最新同类型,同外键分组消息的未读条数
        $foreignType        = $requestInfo->foreign_type;
        $foreignKey         = $requestInfo->foreign_key;
        $where              = [
            'foreign_type'=>$foreignType,
            'foreign_key'=>$foreignKey,
            'to_user_id'=>$userId,
            'is_read'=>0,
        ];
        $number             = UserMessage::getCount($where);
        //查询最新同类型,同外键分组最新的一条信息的相关内容
        $where              = [
            'foreign_type'=>$foreignType,
            'foreign_key'=>$foreignKey,
            'to_user_id'=>$userId
        ];

        $curCount             = UserMessage::getCount($where);
        if($curCount == 0){
            return [
                'code' => 1,
                'is_delete' => 1,
                'message'=> '已删除'
            ];
        }

        $newMessageInfo     = UserMessage::getInfo($where);
        $messageIdArr       = [$newMessageInfo->id];
        $lists              = UserMessage::transformListByIdArr($messageIdArr,$userId);
        $result             = isset($lists[0]) ? $lists[0] : [];
        if($result)
        {
            $result['id']   = $requestMessageId;
        }
        return $result;
    }

}